Skip to content

Fix: roll back meta document and storage instance when createRxCollection() fails in addCollections() - #9595

Merged
pubkey merged 4 commits into
masterfrom
claude/vigilant-newton-8rqsab-failed-collection-creation-leaves-meta-doc
Nov 17, 2026
Merged

pubkey merged 4 commits into
masterfrom
claude/vigilant-newton-8rqsab-failed-collection-creation-leaves-meta-doc

Conversation

@pubkey

@pubkey pubkey commented Nov 11, 2026

Copy link
Copy Markdown
Owner

This PR contains:

  • A BUGFIX

Describe the problem you have without this PR

addCollections() left the collection meta document and the storage instance behind when the creation of the collection failed after the storage instance was created (for example with COL17), so that adding the collection again with an adjusted schema threw DB6

Covers a failure inside createRxCollection() after the storage instance exists, for example COL17. pubkey/rxdb#9577 covers the case where the storage rejects the creation of the storage instance and changes the same block.

  • Not rolled back: a collection that has a meta document of a previous schema version. A failed migration can have written documents into the new storage instance.
  • Risk with multiInstance: when another tab adds the same collection at the same moment and succeeds while the creation in this tab fails, the rollback removes the storage instance and meta document that the other tab uses.
  • The test triggers the failure with a reserved static name, which the dev-mode plugin rejects with COL17.

Changed files:

  • orga/changelog/fix-failed-collection-creation-leaves-meta-document.json
  • src/rx-database.ts
  • test/unit/rx-database.test.ts

Test output without the fix

The changed test file run on the memory storage with src/ from master:

  20 passing (95ms)
  1 failing

  1) rx-database.test.ts
       .addCollections()
         positive
           should be able to add the collection with an adjusted schema after the first creation failed:
     RxError (DB6): 

Error message: RxDatabase.addCollections(): another instance created this collection with a different schema. Read this: https://rxdb.info/rx-schema.html?console=qa#faq
Error code: DB6
Cause: The schema hash does not match the schema stored in the internal database.
Fix: If you changed the schema, you must increment the version number. If not, check why the hash is different.
Docs: https://rxdb.info/rx-schema.html?console=errors&code=DB6#faq
Find out more about this error here: https://rxdb.info/errors.html?console=errors#DB6 
Still stuck? Ask in the RxDB Discord: https://rxdb.info/chat 

--------------------
Parameters:
database: "xvdeadxksr"
collection: "items"
previousSchemaHash: "e1db93378e0a3a31dfb44b89b834ed1fe7279e59b9d48ef6455999f00a4aebe8"
schemaHash: "f9d03b4eb5518c419ace2eafc1d744d2d3df5c01abc86b1750503837f298933b"
previousSchema: {
  "additionalProperties": false,
  "description": "describes a human being",
  "encrypted": [],
  "indexes": [
    [
      "_deleted",
      "firstName",
      "passportId"

Test output with the fix

39 passing (214ms)

Todos

  • Tests
  • Changelog

Assisted by

YES, CLAUDE CODE


Generated by Claude Code

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Verify Test Reproduction: Tests FAILED without the fix (expected)

This confirms the changed tests correctly reproduce the bug that the source changes fix.

This workflow runs the changed tests without the source fix to verify they reproduce the bug.

Show output
...(truncated, showing last 200 of 1809 lines)
	    "lastName",
	    "passportId",
	    "_deleted",
	    "_rev",
	    "_meta",
	    "_attachments"
	  ],
	  "title": "human schema",
	  "type": "object",
	  "version": 0
	}
	schema: {
	  "title": "human schema",
	  "description": "changed",
	  "version": 0,
	  "keyCompression": false,
	  "primaryKey": "passportId",
	  "type": "object",
	  "properties": {
	    "passportId": {
	      "type": "string",
	      "maxLength": 100
	    },
	    "firstName": {
	      "type": "string",
	      "maxLength": 100
	    },
	    "lastName": {
	      "type": "string",
	      "maxLength": 100
	    },
	    "age": {
	      "description": "age in years",
	      "type": "integer",
	      "minimum": 0,
	      "maximum": 150,
	      "multipleOf": 1
	    }
	  },
	  "required": [
	    "firstName",
	    "lastName",
	    "passportId"
	  ],
	  "indexes": [
	    "firstName"
	  ]
	}
	
	    at newRxError (/home/runner/work/rxdb/rxdb/src/rx-error.ts:127:12 <- /tmp/_karma_webpack_rxdb_5835/commons.js:191300:10)
	    at /home/runner/work/rxdb/rxdb/src/rx-database.ts:484:41 <- /tmp/_karma_webpack_rxdb_5835/commons.js:190175:74
	    at async Promise.all (index 0)
	    at async RxDatabaseBase.addCollections (/home/runner/work/rxdb/rxdb/src/rx-database.ts:461:13 <- /tmp/_karma_webpack_rxdb_5835/commons.js:190155:7)
	    at async Context.<anonymous> (/home/runner/work/rxdb/rxdb/test/unit/rx-database.test.ts:462:37 <- /tmp/_karma_webpack_rxdb_5835/commons.js:223885:27)


Chrome Headless 157.0.0.0 (Linux 0.0.0): Executed 419 of 1507�[31m (1 FAILED)�[39m (7.091 secs / 6.494 secs)
�[31mTOTAL: 1 FAILED, 418 SUCCESS�[39m


�[31m1) should be able to add the collection with an adjusted schema after the first creation failed
�[39m�[31m     rx-database.test.ts .addCollections() positive
�[39m�[90m     RxError (DB6): 

Error message: RxDatabase.addCollections(): another instance created this collection with a different schema. Read this: https://rxdb.info/rx-schema.html?console=qa#faq
Error code: DB6
Cause: The schema hash does not match the schema stored in the internal database.
Fix: If you changed the schema, you must increment the version number. If not, check why the hash is different.
Docs: https://rxdb.info/rx-schema.html?console=errors&code=DB6#faq
Find out more about this error here: https://rxdb.info/errors.html?console=errors#DB6 
Still stuck? Ask in the RxDB Discord: https://rxdb.info/chat 

--------------------
Parameters:
database: "etpfmclxjq"
collection: "items"
previousSchemaHash: "e1db93378e0a3a31dfb44b89b834ed1fe7279e59b9d48ef6455999f00a4aebe8"
schemaHash: "f9d03b4eb5518c419ace2eafc1d744d2d3df5c01abc86b1750503837f298933b"
previousSchema: {
  "additionalProperties": false,
  "description": "describes a human being",
  "encrypted": [],
  "indexes": [
    [
      "_deleted",
      "firstName",
      "passportId"
    ],
    [
      "_meta.lwt",
      "passportId"
    ]
  ],
  "keyCompression": false,
  "primaryKey": "passportId",
  "properties": {
    "_attachments": {
      "type": "object"
    },
    "_deleted": {
      "type": "boolean"
    },
    "_meta": {
      "additionalProperties": true,
      "properties": {
        "lwt": {
          "maximum": 1000000000000000,
          "minimum": 1,
          "multipleOf": 0.01,
          "type": "number"
        }
      },
      "required": [
        "lwt"
      ],
      "type": "object"
    },
    "_rev": {
      "minLength": 1,
      "type": "string"
    },
    "age": {
      "description": "age in years",
      "maximum": 150,
      "minimum": 0,
      "multipleOf": 1,
      "type": "integer"
    },
    "firstName": {
      "maxLength": 100,
      "type": "string"
    },
    "lastName": {
      "maxLength": 100,
      "type": "string"
    },
    "passportId": {
      "maxLength": 100,
      "type": "string"
    }
  },
  "required": [
    "firstName",
    "lastName",
    "passportId",
    "_deleted",
    "_rev",
    "_meta",
    "_attachments"
  ],
  "title": "human schema",
  "type": "object",
  "version": 0
}
schema: {
  "title": "human schema",
  "description": "changed",
  "version": 0,
  "keyCompression": false,
  "primaryKey": "passportId",
  "type": "object",
  "properties": {
    "passportId": {
      "type": "string",
      "maxLength": 100
    },
    "firstName": {
      "type": "string",
      "maxLength": 100
    },
    "lastName": {
      "type": "string",
      "maxLength": 100
    },
    "age": {
      "description": "age in years",
      "type": "integer",
      "minimum": 0,
      "maximum": 150,
      "multipleOf": 1
    }
  },
  "required": [
    "firstName",
    "lastName",
    "passportId"
  ],
  "indexes": [
    "firstName"
  ]
}

    at newRxError (/home/runner/work/rxdb/rxdb/src/rx-error.ts:127:12 <- /tmp/_karma_webpack_rxdb_5835/commons.js:191300:10)
    at /home/runner/work/rxdb/rxdb/src/rx-database.ts:484:41 <- /tmp/_karma_webpack_rxdb_5835/commons.js:190175:74
    at async Promise.all (index 0)
    at async RxDatabaseBase.addCollections (/home/runner/work/rxdb/rxdb/src/rx-database.ts:461:13 <- /tmp/_karma_webpack_rxdb_5835/commons.js:190155:7)
    at async Context.<anonymous> (/home/runner/work/rxdb/rxdb/test/unit/rx-database.test.ts:462:37 <- /tmp/_karma_webpack_rxdb_5835/commons.js:223885:27)
�[39m



View full workflow run

@pubkey
pubkey force-pushed the claude/vigilant-newton-8rqsab-failed-collection-creation-leaves-meta-doc branch from 5dbb5fb to 01ff046 Compare November 12, 2026 12:08

pubkey commented Nov 13, 2026

Copy link
Copy Markdown
Owner Author

Pushed 8169fb9. It removes the migration test that built its own Dexie.js storage. I first moved it to config.storage.getStorage(), but there it does not fail without the fix on any storage, and it also passed with the guarded line of the fix removed. So it did not test the fix. The other test of this pull request in rx-database.test.ts already uses the storage of the test config. It fails without the fix and passes with it on memory, memory-random-delay, Dexie.js, localStorage, and remote.

A separate finding from that attempt, not changed here: on the memory storage addCollections() does not settle when the database is closed during an auto migration. The migration is canceled and migratePromise() waits for a status that never comes. This is the same on master.


Generated by Claude Code

@pubkey
pubkey force-pushed the claude/vigilant-newton-8rqsab-failed-collection-creation-leaves-meta-doc branch from 8169fb9 to 25821fd Compare November 13, 2026 12:56
…tion() fails in addCollections()

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F
… closed before the rollback

The rollback of a failed collection creation is skipped when the database is closing or closed, and an error inside the rollback no longer replaces the creation error.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F
The test built its own dexie storage on fake-indexeddb. With the storage
of the test config it never settles on the memory storage, with and
without the fix, because addCollections() does not settle when the
database is closed during a migration there. On dexie, localstorage and
remote it passes also without the fix, so it does not show the bug on
any storage. The fix stays covered by the test in rx-database.test.ts,
which uses the storage of the test config and fails without the fix on
each storage.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F
@pubkey
pubkey force-pushed the claude/vigilant-newton-8rqsab-failed-collection-creation-leaves-meta-doc branch from 25821fd to 58bcb66 Compare November 14, 2026 13:21
createRxCollection() closed the storage instance of a failed collection
but left the collection itself open. With the rollback of addCollections()
this caused two failures:
- The failed collection still listened to the internal store. When the
  rollback deleted the meta document, the failed collection called close()
  and removed the later collection with the same name from
  database.collections, so database.close() no longer closed it
  (last.test.ts "ensure all collections are closed").
- The cleanup plugin kept running on the failed collection and called
  cleanup() on the removed storage (sqlite: "no such table: items-0").

The rollback code is also written shorter so that the memory-minimal
bundle stays inside its size limit (146873 bytes, limit 146885).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F
@pubkey
pubkey merged commit 044ef2a into master Nov 17, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants